package com.libre.auth.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.libre.auth.entity.Depart;
import com.libre.auth.entity.User;
import com.libre.auth.service.DepartService;
import com.libre.auth.service.UserService;
import com.libre.common.base.BaseController;
import com.libre.common.enums.ResultEnum;
import com.libre.common.utils.PageUtil;
import com.libre.common.utils.ResultUtil;
import com.libre.common.vo.PageVo;
import com.libre.common.vo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 庞留杰
 * @version V1.0
 * @description 用户管理
 * @date 2020/7/23 8:59
 **/
@Slf4j
@RequestMapping("/user")
@RestController
public class UserController extends BaseController {

    @Autowired
    private UserService userService;
    @Autowired
    private DepartService departService;

    @GetMapping("/findPage")
    public Result<IPage<User>> findPage(@ModelAttribute PageVo pageVo, @ModelAttribute User user) {
        System.out.println("用户信息==========="+ getLoginUser());
        IPage<User> page = userService.findPage(PageUtil.initMpPage(pageVo), user);
        return new ResultUtil<IPage<User>>().setData(page);
    }

    @GetMapping("/findList")
    public Result<List<User>> findList(@ModelAttribute User user) {
        List<User> list = userService.findList(user);
        return new ResultUtil<List<User>>().setData(list);
    }

//    @GetMapping("/findById")
//    public Result<User> findById(String id) {
//        User user = userService.getById(id);
//        return new ResultUtil<User>().setData(user);
//    }
//
//    @PostMapping("/save")
//    public Result save(@RequestBody User user) {
//        userService.save(user);
//        return new ResultUtil().setSuccessMsg(ResultEnum.OK.getReasonPhraseCN());
//    }

    @PostMapping("/addOrUpdate")
    public Result addOrUpdate(@RequestBody User user) {
        userService.addOrUpdate(user);
        return new ResultUtil().setSuccessMsg(ResultEnum.OK.getReasonPhraseCN());
    }

//
//
//    @PostMapping("/update")
//    public Result update(@RequestBody User user) {
//        VerifyUtil.notEmpty("id",user.getId());
//        return new ResultUtil().setSuccessMsg(ResultEnums.OK.getReasonPhraseCN());
//    }
//
//
//    @ApiOperation(value = "删除", notes = "删除【逻辑删除】")
//    @PostMapping("/delete/{id}")
//    public Result delete(@PathVariable String id) {
//        User user = new User();
//        user.setId(id);
//        user.setIs_delete("1");
//        userService.updateById(user);
//        QueryWrapper<UserRole> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("user_id", id);
//        userRoleService.remove(queryWrapper);
//        return new ResultUtil().setSuccessMsg(ResultEnums.OK.getReasonPhraseCN());
//    }

    //部门树
    @GetMapping("/departTree")
    public Result<List<Depart>> departTree(@ModelAttribute Depart depart) {
        List<Depart> departTree = departService.departTree(depart);
        return new ResultUtil<List<Depart>>().setData(departTree);
    }

}
